package com.example.xiaomiao.Service.Impl;

import com.example.xiaomiao.DO.Note;
import com.example.xiaomiao.DTO.*;
import com.example.xiaomiao.Mapper.NoteMapper;
import com.example.xiaomiao.Service.NoteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class NoteServiceImpl implements NoteService {
    @Resource
    NoteMapper noteMapper;
    private static final Logger logger = LoggerFactory.getLogger(NoteServiceImpl.class);

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Return add(int userId, NoteAddRequestDTO noteAddRequestData) {
        try {
            return add1(userId,noteAddRequestData);
        } catch (Exception e){
            return Return.fail(1,"添加失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Return delete(NoteDeleteRequestDTO noteDeleteRequestData) {
        try {
            return delete1(noteDeleteRequestData);
        } catch (Exception e){
            return Return.fail(1,"删除失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Return update(Note note) {
        try {
            return update1(note);
        } catch (Exception e){
            return Return.fail(1,"修改失败");
        }
    }

    @Override
    public Return get(int userId, GetNoteRequestDTO getNoteRequestData) {
        int total = noteMapper.getNoteNum(userId,getNoteRequestData.getDate());
        if(total == 0)
            return Return.ok("还没有备忘录",new ListResponseDTO(total,new ArrayList<>()));
        int start = getNoteRequestData.getStart_index();
        int count = getNoteRequestData.getCount();
        List<Note> notes = noteMapper.getNotes(userId,getNoteRequestData.getDate(),start,Math.min(count,total-start));
        if(notes.isEmpty())
            return Return.ok("这一页还没有备忘录",new ListResponseDTO(total,notes));
        return Return.ok(new ListResponseDTO(total,notes));
    }

    @Transactional(rollbackFor = Exception.class)
    public Return add1(int userId, NoteAddRequestDTO noteAddRequestData) {
        try{
            noteMapper.addNotes(userId,noteAddRequestData.getDate(),noteAddRequestData.getNote());
            return Return.ok();
        } catch (Exception e){
            logger.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Return delete1(NoteDeleteRequestDTO noteDeleteRequestData) {
        try {
            noteMapper.deleteNotes(noteDeleteRequestData.getNote_ids());
            return Return.ok();
        } catch (Exception e){
            logger.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public Return update1(Note note) {
        try {
            noteMapper.updateNote(note);
            return Return.ok();
        } catch (Exception e){
            logger.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
